package cxy.highConcurrency;

/**
 * 演示Happens-Before 规则
 * @author admin
 *
 */
class VolatileExample {
	
	static int x = 0;
	static volatile boolean v = false;
	
	public static void main(String[] args) {
		Thread threadA = new Thread(() ->  {
			writer();
		});
		
		Thread threadB = new Thread(() ->  {
			reader();
		});
		
		threadA.start();
		threadB.start();
		
	}
	
	public static void writer() {
		x = 42;
		v = true;
	}

	public static void reader() {
		if (v == true) {
			System.out.println("x:" + x);
		}
	}
}